package org.andao.commons.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.andao.commons.model.Attachs;
import org.andao.commons.service.AttachsService;
import org.andao.commons.web.FileUriContext;
import org.andao.core.base.service.BaseServiceImpl;
import org.andao.core.exception.ServiceException;
import org.andao.core.utils.FileUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;



/**
 * 公共附件接口实现.
 * 
 * @author Marco.hu(hzg139@163.com)
 */
@Service("attachsService")
@Transactional
public class AttachsServiceImpl extends BaseServiceImpl<Attachs> implements AttachsService {

	public void updateAttachByFormId(String[] attachIds, String formId, String formType,String fillinManId,String fillinMan) throws ServiceException {
		if(null != attachIds && attachIds.length >0){
			for(int i = 0;i<attachIds.length;i++){
				Attachs attachs = get(attachIds[i]);
				attachs.setFormId(formId);
				attachs.setFormType(formType);
				attachs.setFillinName(fillinMan);
				attachs.setFillinNameId(fillinManId);		
				update(attachs);
				logger.info("更新附件...,{}",attachs.getAttName());
			}
		}
		
	}

	public Object saveOrUpdateWithEntity(Attachs attach,Object object){
		if(null != attach){
			File tempFile = new File(FileUriContext.instance().getServerStoreTempPath(),attach.getSerName());//
			File targetFolder = new File(attach.getTargetPath());		
			if (!targetFolder.exists()) {
				targetFolder.mkdirs();
			}		
			
			try {
				FileUtils.copyFile(tempFile, new File(targetFolder,attach.getSerName()));
			} catch (IOException e) {
				throw new ServiceException("拷贝文件出错："+e);
			}
		}
				
		exGeneralHibernateDao.saveOrUpdate(object);		
		// 清理附件
		if(null != attach) deleteAttach(attach.getResourceid());		
		return object;
	}
	
	public List<Attachs> findAttachsByFormId(String formId) throws ServiceException {
		List<Attachs> attachs = findByHql(" from "+Attachs.class.getName()+" where isDeleted=0 and formId=? ", formId);
		return attachs;
	}
	
	public List<Attachs> findAttachsByFillInManIdAndTypes(String id, String[] types) throws ServiceException{
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("fillinManId", id);
		values.put("types", types);
		StringBuffer hql = new StringBuffer(" from "+Attachs.class.getName()+" where isDeleted=0 and fillinManId=:fillinManId and attType in (:types)");
		
		List<Attachs> attachs = findByHql(hql.toString(), values);
		return attachs;
	}
	

	@Override
	public void deleteAttach(String attachId) throws ServiceException {
		Attachs attach = get(attachId);
		String attachPath = attach.getSerPath()+File.separator+attach.getSerName();
		//删除附件
		try {
			FileUtils.delFile(attachPath);
		} catch (IOException e) {
			logger.warn("没有删除附件:{}",attachPath);
		}
		truncate(get(attachId));
	}

	
}
